package jgl.vajra.operator.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;


import jgl.vajra.operator.R;
import jgl.vajra.operator.databinding.ViewMyToolsbarBinding;
import jgl.vajra.operator.listener.MyToolbarViewListener;
import jgl.vajra.operator.widget.base.BaseCustomView;
import jgl.vajra.operator.widget.dialogfragment.ShareDialogFragment;
import jgl.vajra.operator.widget.utils.ToolbarUtils;
import jgl.vajra.operator.widget.utils.UIUtils;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.BindingAdapter;

public class MyToolbarView extends BaseCustomView<ViewMyToolsbarBinding> {
    private MyToolbarViewListener myToolbarViewListener;
    private boolean toolbarRightShare;
    private ShareDialogFragment shareFragment;

    public MyToolbarView(Context context) {
        super(context);
    }

    public MyToolbarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_my_toolsbar;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        mBinding.setView(this);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyToolbarView);
        int toolbarBackground = typedArray.getResourceId(R.styleable.MyToolbarView_toolbarBackground, R.color.titleBackgroundColor);
        mBinding.toolbar.setBackgroundResource(toolbarBackground);
        toolbarRightShare = typedArray.getBoolean(R.styleable.MyToolbarView_toolbarRightShare, false);
        boolean imageRightShow = typedArray.getBoolean(R.styleable.MyToolbarView_toolbarImageRightShow, toolbarRightShare);
        boolean isToolbarLineShow = typedArray.getBoolean(R.styleable.MyToolbarView_toolbarLineShow, true);
        if (imageRightShow) {
            setRightImageViewVisibility(true);
        }

        int imageRightRes = typedArray.getResourceId(R.styleable.MyToolbarView_toolbarImageRightRes, -1);

        if (toolbarRightShare) {
            imageRightShow = true;
            //imageRightRes = R.drawable.ic_black_share;
        }
        if (imageRightShow) {
            setRightImageBackgroundResource(imageRightRes);
        }
        boolean textRightShow = typedArray.getBoolean(R.styleable.MyToolbarView_toolbarTextRightShow, false);
        if (textRightShow) {
            setRightTextViewVisibility(true);
        }
        boolean toolbarImageLeftShow = typedArray.getBoolean(R.styleable.MyToolbarView_toolbarImageLeftShow, true);
        if (toolbarImageLeftShow) {
            int imageLeftRes = typedArray.getResourceId(R.styleable.MyToolbarView_toolbarImageLeftRes, R.drawable.ic_left_arrow);
            ToolbarUtils.initToolBarByIcon(mBinding.toolbar, (AppCompatActivity) context, imageLeftRes);
        }

        String titleText = typedArray.getString(R.styleable.MyToolbarView_toolbarTitleText);
        setTitleText(titleText);

        String toolbarTextRightText = typedArray.getString(R.styleable.MyToolbarView_toolbarTextRightText);
        if (textRightShow && !TextUtils.isEmpty(toolbarTextRightText)) {
            setTextRightText(toolbarTextRightText);
            int toolbarTextRightTextColor = typedArray.getColor(R.styleable.MyToolbarView_toolbarTextRightTextColor, UIUtils.getColor(R.color.black_33));
            setToolbarTextRightTextColor(toolbarTextRightTextColor);
        }


        typedArray.recycle();
    }

    public void setToolbarTextRightTextColor(int toolbarTextRightTextColor) {
        mBinding.tvRight.setTextColor(toolbarTextRightTextColor);

    }

    public void setTextRightText(String toolbarTextRightText) {
        mBinding.tvRight.setText(toolbarTextRightText);
    }

    public void setRightTextViewVisibility(boolean b) {
        if (b)
            mBinding.tvRight.setVisibility(VISIBLE);
        else
            mBinding.tvRight.setVisibility(GONE);
    }

    public void setRightTextViewVisibility(int b) {
        mBinding.tvRight.setVisibility(b);
    }

    public void setTitleText(String titleText) {
        if (!TextUtils.isEmpty(titleText)) {
            mBinding.tvTitle.setText(titleText);
        }

    }
    public void setTitleColor(int  titleText) {
            mBinding.tvTitle.setTextColor(UIUtils.getColor(titleText));

    } public void setTitleLeft(int  titleText) {
            mBinding.tvTitle.setTextColor(UIUtils.getColor(titleText));

    }

    public void setRightImageBackgroundResource(int imageRightRes) {
        mBinding.ivRight.setBackgroundResource(imageRightRes);
    }

    public void setToolsBarBackgroundColor(int imageRightRes) {
        mBinding.toolbar.setBackgroundColor(imageRightRes);
    }

    public void setToolsBarAlpha(float imageRightRes) {
        mBinding.toolbar.setAlpha(imageRightRes);
    }

    public void setRightImageViewVisibility(boolean b) {
        if (b)
            mBinding.ivRight.setVisibility(VISIBLE);
        else
            mBinding.ivRight.setVisibility(GONE);

    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    public void setMyToolbarViewListener(MyToolbarViewListener listener) {
        this.myToolbarViewListener = listener;
    }

    public void onRightTextClick() {
        if (myToolbarViewListener != null) myToolbarViewListener.onRightTextClick();
    }


    public void onRightImageClick() {
      /*  if (toolbarRightShare && shareInfo != null) {
            FragmentActivity context = (FragmentActivity) getContext();
            if (shareFragment == null)
                shareFragment = FragmentManager.getShareFragment(context, shareInfo, ShareShowBean.getShareBookDetailsShowBean());
            shareFragment.show(context.getSupportFragmentManager());
        }*/
        if (myToolbarViewListener != null) myToolbarViewListener.onRightImageClick();
    }

    @BindingAdapter("onToolbarTitle")
    public static void onToolbarTitle(MyToolbarView toolbarView, String res) {
        toolbarView.setTitleText(res);
    }


    public TextView getTitleView() {
        return mBinding.tvTitle;
    }

    public Toolbar getToolsBar() {
        return mBinding.toolbar;
    }
}
